define(['jquery', 'ngConstant', 'ngAlert'], function (jquery, ngConstant, ngAlert) {
    return function ($http, params, successFn, alwaysFn, isFormPost) {
        var ngIndex = ngAlert.load();
        var host = ngConstant.interfaceHost;
        var option = {
            url: "",
            data: {},
            type: "POST",
            headers: {'Content-Type': 'application/json;charset=utf-8'},
            responseType: "json"
        };
        if (!isFormPost) {
            //服务器端口
            // console.log("json");
            jquery.extend(option, params);
            // var formatData=[];
            // angular.forEach(option.data,function(value,key){
            //     formatData.push(key);
            //     formatData.push("=");
            //     formatData.push(value);
            //     formatData.push("&");
            // });
            //  var dataForm =formatData.join('');
            // dataForm =  dataForm.substr(0,dataForm.length-1);
            // console.log(dataForm)
            $http({
                url: host + option.url,
                withCredentials: true,
                method: option.type,
                data: option.data,
                headers: option.headers,
                responseType: option.responseType
            }).then(function (response) {
                    ngAlert.close(ngIndex);
                    if (typeof  successFn === "function") {
                        successFn(response.data);
                    }
                    if (typeof  alwaysFn === "function") {
                        alwaysFn();
                    }
                },
                function () {
                    if (typeof  successFn === "function") {
                        ngAlert.msg("服务器错误");
                        if (typeof  alwaysFn === "function") {
                            alwaysFn();
                        }
                    }
                });
        }
        else {
            //服务器端口;
            option = {
                url: "",
                data: {},
                type: "POST",
                withCredentials: true,
                headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'},
                responseType: "json"
            };
            jquery.extend(option, params);
            var formatData = [];
            angular.forEach(option.data, function (value, key) {
                formatData.push(key);
                formatData.push("=");
                formatData.push(value);
                formatData.push("&");
            });
            var dataForm = formatData.join('');
            dataForm = dataForm.substr(0, dataForm.length - 1);
            $http({
                url: host + option.url,
                method: option.type,
                data: dataForm,
                headers: option.headers,
                responseType: option.responseType
            }).then(function (response) {
                    ngAlert.close(ngIndex);
                    if (typeof  successFn === "function") {
                        successFn(response.data);
                    }
                    if (typeof  alwaysFn === "function") {
                        alwaysFn();
                    }
                },
                function () {
                    if (typeof  successFn === "function") {
                        ngAlert.msg("服务器错误");
                        if (typeof  alwaysFn === "function") {
                            alwaysFn();
                        }
                    }
                });
        }
    }
});